package org.dromara.hrp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.web.core.BaseService;
import org.dromara.hrp.domain.HrpLead;
import org.dromara.hrp.domain.HrpPartner;
import org.dromara.hrp.mapper.HrpPartnerMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 联系人合作伙伴Service业务层处理
 *
 * @author wenrui Su
 * @date 2024-05-27
 */
@RequiredArgsConstructor
@Service
public class HrpPartnerServiceImpl extends BaseService {

  private final HrpPartnerMapper baseMapper;

  /**
   * 查询联系人合作伙伴
   */
  public HrpPartner queryById(Long id){
    return baseMapper.selectById(id);
  }

  /**
   * 查询联系人合作伙伴列表
   */
  public Page<HrpPartner> queryPageList(HrpPartner bo, PageQuery pageQuery) {
    LambdaQueryWrapper<HrpPartner> lqw = buildQueryWrapper(bo);
    lqw.orderByDesc(HrpPartner::getUpdateTime);
    Page<HrpPartner> result = baseMapper.selectPage(pageQuery.build(), lqw);
    return result;
  }

  /**
   * 查询联系人合作伙伴列表
   */
  public List<HrpPartner> queryList(HrpPartner bo) {
    LambdaQueryWrapper<HrpPartner> lqw = buildQueryWrapper(bo);
    return baseMapper.selectList(lqw);
  }

  private LambdaQueryWrapper<HrpPartner> buildQueryWrapper(HrpPartner bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<HrpPartner> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), HrpPartner::getName, bo.getName());
    lqw.eq(StringUtils.isNotBlank(bo.getMobile()), HrpPartner::getMobile, bo.getMobile());
    lqw.eq(bo.getIsCustomer() != null, HrpPartner::getIsCustomer, bo.getIsCustomer());
    lqw.eq(bo.getIsSupplier() != null, HrpPartner::getIsSupplier, bo.getIsSupplier());
    lqw.eq(bo.getIsManufacturer() != null, HrpPartner::getIsManufacturer, bo.getIsManufacturer());
    lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
      HrpPartner::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
    lqw.orderByDesc(params.get("endUpdateTime") != null,HrpPartner::getUpdateTime);
    return lqw;
  }

  /**
   * 新增联系人合作伙伴
   */
  public Boolean insertByBo(HrpPartner bo) {
    validEntityBeforeSave(bo);
    boolean flag = baseMapper.insert(bo) > 0;
    return flag;
  }

  /**
   * 修改联系人合作伙伴
   */
  public Boolean updateByBo(HrpPartner bo) {
    validEntityBeforeSave(bo);
    return baseMapper.updateById(bo) > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(HrpPartner entity){
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除联系人合作伙伴
   */
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if(isValid){
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
